package com.gitee.openviducn.inspector.rest;

import com.gitee.openviducn.inspector.auth.AuthAction;
import com.gitee.openviducn.inspector.auth.AuthService;
import com.gitee.openviducn.inspector.auth.JwtUtil;
import com.gitee.openviducn.inspector.auth.SimpleWebAuthServiceImpl;
import com.gitee.openviducn.inspector.common.BizException;
import com.gitee.openviducn.inspector.common.ResponseError;
import com.gitee.openviducn.inspector.common.ResponseResult;
import com.gitee.openviducn.inspector.domain.vo.request.LoginRequest;
import com.gitee.openviducn.inspector.domain.vo.response.LoginResponse;
import com.gitee.openviducn.inspector.domain.vo.response.UserInfoResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 02.24 2020
 */
@Api(tags = "登录登出相关接口")
@RestController
public class LoginController extends BaseController{

    @Value("${auth.username:openviducn}")
    private String authUsername;

    @Value("${auth.password:openviducn}")
    private String authPassword;

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public ResponseResult<LoginResponse> login(@Validated @RequestBody LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();
        Assert.notNull(username,"帐号不能空");
        Assert.notNull(password,"密码不能空");

        if (!Objects.equals(authUsername, username) || !Objects.equals(authPassword, password)) {
            return ResponseResult.error(ResponseError.AUTH_CA_ERROR, "帐号或密码错误");
        }

        AuthService.AuthUser authUser = new SimpleWebAuthServiceImpl.SimpleWebAuthUserImpl(1L, username, AuthService.SystemRole.ADMIN);
        String token = JwtUtil.signWithExpireTime(authUser.toMap(), null, null);
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setToken(token);
        return ResponseResult.success(loginResponse);
    }

    @ApiOperation("退出接口")
    @PostMapping("/logout")
    public ResponseResult<Void> login(HttpServletRequest request) {
        // TODO 退出数据清退
        return ResponseResult.success();
    }

    @ApiOperation("帐号信息接口")
    @AuthAction
    @PostMapping("/userinfo")
    public ResponseResult<UserInfoResponse> userinfo(HttpServletRequest request) throws BizException {
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        userInfoResponse.setId(getUserId(request));
        userInfoResponse.setNickname(getUsername(request));
        userInfoResponse.setUsername(getUsername(request));
        return ResponseResult.success(userInfoResponse);
    }

}
